package wiki.xsx.excutor;

import cn.hutool.core.util.PageUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Page;
import cn.hutool.setting.dialect.Props;
import com.alibaba.druid.pool.DruidDataSource;
import wiki.xsx.config.Target;
import wiki.xsx.exception.DatasyncException;
import wiki.xsx.task.Task;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @Descript
 * @Author xsx
 * @Date 2018/1/17
 */
public class Excutor {
    private static DruidDataSource dataSource;
    private static List<Target> targets;
    {
        Properties properties = Props.getProp("application.properties", "utf-8");
        dataSource = new DruidDataSource();
        dataSource.setDriverClassName(check(properties.getProperty("datasource.driver-class"), "'datasource.driver-class'不能为空"));
        dataSource.setUrl(check(properties.getProperty("datasource.url"), "'datasource.url'不能为空"));
        dataSource.setUsername(check(properties.getProperty("datasource.username"), "'datasource.username'不能为空"));
        dataSource.setPassword(check(properties.getProperty("datasource.password"), "'datasource.password'不能为空"));

        targets = new ArrayList<>();
    }

    public static void excute(){
        for (Target target : targets){
            new Task().start(target);
        }
        PageUtil
    }

    public static DruidDataSource getDataSource(){
        return dataSource;
    }

    private static String check(String value, String errorMessage){
        if (StrUtil.isBlank(value)){
            throw new DatasyncException(errorMessage);
        }
        return value;
    }
}
